<script setup>
import {onMounted, ref} from 'vue'
import { Timer } from '@element-plus/icons-vue'
import {getRoleByPage, saveRole, updateSysRole, deleteSysRole, findSysRoleMenuByRoleId,assginRoleMenu} from "@/api/sysRole"
import {ElMessage} from "element-plus";

const  centerDialogVisible = ref(false);
const total = ref(0)



const formInline = ref({
  roleName: '',
  pageNum: 1,
  pageSize: 10,
})

const tree = ref();
const defaultProps = {
  children: 'children',
  label: 'title',
}
const menusTree = ref([])
const  checkedMenuIds = ref([]);


//获取所有菜单和获取当前角色的选择的菜单

const showMenu = (row)=>{
  dialogData.value.id =row.id
  findSysRoleMenuByRoleId(dialogData.value.id).then(res=>{
    menusTree.value =  res.data.sysMenus
    checkedMenuIds.value = res.data.roleCheckedIds
  })
}


const  dialogFormVisible = ref(false)
onMounted(()=>{

  getListByPage()
})
const resetForm = ()=>{
  formInline.value.roleName = ''
}



const onSubmit = () => {
  getListByPage()
}
const tableData = ref([
  {

  }
])
const dialogData = ref({
  id:'',
  roleName: '',
  roleCode:'',
  description:''

})

const resetDialogData = ()=>{
  dialogData.value.roleName = ''
  dialogData.value.roleCode = ''
  dialogData.value.description = ''
  dialogData.value.id = ''

}
//分页擦查询角色
const getListByPage = () => {
  console.log(formInline.value)
  getRoleByPage(formInline.value).then((res) => {
    tableData.value = res.data.list
    total.value = res.data.total
  })
}

const handleSizeChange = ()=>{
  getListByPage()
}

const handleCurrentChange =()=>{
  getListByPage()
}

const submitForm = ()=>{

  //通过有没有id判断是新增还是修改
  if (dialogData.value.id == "") {
    //新增
    saveRole(dialogData.value).then(res=>{
      ElMessage.success(res.message)
      getListByPage()
      resetDialogData()
    })
  }else {
    //修改
    updateSysRole(dialogData.value).then(res=>{
      ElMessage.success(res.message)
      getListByPage()
      resetDialogData()
    })
  }
}
//修改
const handleEdit = (row)=>{
  dialogData.value.id = row.id
  dialogData.value.roleName = row.roleName
  dialogData.value.roleCode = row.roleCode
  dialogData.value.description = row.description
}

//删除
let handleDelete = (row)=>{
  deleteSysRole(row).then(res=>{
    ElMessage.success(res.message)
    getListByPage()
  })
}
const submitRoleMeal = ()=>{

  //获取选择的roleId
  const roleId = dialogData.value.id
  //获取全选重节点
  const checkedNodes = tree.value.getCheckedNodes().map(node=>{
    return {
      menuId:node.id,
      isHalf:0
    }
  })
  //获取半选中节点
  const halfCheckedNodes = tree.value.getHalfCheckedNodes().map(node=>{
    return {
      menuId:node.id,
      isHalf:1
    }
  })
  //合并全选中节点和半选中节点
  const allNode = [...checkedNodes,...halfCheckedNodes]

  //组合节点和roleId
  const assginMenuDto = {
    roleId :roleId,
    menuIdList:allNode
  }
  console.log(assginMenuDto)
  //发起请求
  assginRoleMenu(assginMenuDto).then(res=>{
    //判断结果
    if (res.code ==200){
      ElMessage.success(res.message)
      //关闭弹窗
      centerDialogVisible.value = false
    }
  })
}
</script>

<template>
  <div class="search-div">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="角色名称:">
        <el-input v-model="formInline.roleName" placeholder="输入角色名" clearable />
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>

    </el-form>
    <el-button type="success" @click="dialogFormVisible = true" >添加</el-button>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="角色名称" width="300">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <span style="margin-left: 10px">{{ scope.row.roleName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="角色code" width="300">
        <template #default="scope">
          <div style="display: flex; align-items: center">

            <span style="margin-left: 10px">{{ scope.row.roleCode }}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="300">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon><timer /></el-icon>
            <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="dialogFormVisible = true,handleEdit(scope.row)">
            修改
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
          <el-button type="warning" plain @click="centerDialogVisible = true,showMenu(scope.row)">
            分配菜单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <hr>
      <el-pagination
          v-model:current-page="formInline.pageNum"
          v-model:page-size="formInline.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

<!--    dialog-->
    <el-dialog v-model="dialogFormVisible" title="添加或删除角色" width="500px">
      <el-form :model="dialogData">
        <el-form-item label="角色名称" :label-width="100">
          <el-input v-model="dialogData.roleName" autocomplete="off" />
        </el-form-item>

          <el-form-item label="角色code" :label-width="100">
            <el-input v-model="dialogData.roleCode" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色描述" :label-width="100">
          <el-input v-model="dialogData.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false,submitForm()">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>

  <div>



      <el-dialog v-model="centerDialogVisible" title="选择菜单" width="50%" center>
          <el-tree
              style="max-width: 600px"
              :data="menusTree"
              ref="tree"
              show-checkbox
              node-key="id"
              default-expand-all
              :default-checked-keys="checkedMenuIds"
              :props="defaultProps"
          />

        <template #footer>
          <div class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="centerDialogVisible = false,submitRoleMeal()">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>